package com.miao.controller
{
	import com.miao.view.DownLoadManagerMediator;
	import com.miao.view.FlvPlayerMediator;
	import com.miao.view.ResourceTreeMediator;
	import com.miao.view.SwfPlayerMediator;
	import com.miao.view.interfaces.IPlayer;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	public class PlayUrlCommand extends SimpleCommand implements ICommand
	{
		
		override public function execute(note:INotification):void{
			var swfPlayer:SwfPlayerMediator = facade.retrieveMediator(SwfPlayerMediator.NAME) as SwfPlayerMediator;
			var flvPlayer:FlvPlayerMediator = facade.retrieveMediator(FlvPlayerMediator.NAME) as FlvPlayerMediator;
			var dlm:DownLoadManagerMediator = facade.retrieveMediator(DownLoadManagerMediator.NAME) as DownLoadManagerMediator;
			var tree:ResourceTreeMediator = facade.retrieveMediator(ResourceTreeMediator.NAME) as ResourceTreeMediator;
			
			var xml:XML = note.getBody() as XML;
			var type:String = xml.name();
			var url:String = xml.@src;
			
			var player:IPlayer;
			if(type=="swf"){
				swfPlayer.show();
				flvPlayer.hide();
				player = swfPlayer;
			}else if(type=="flv"){
				swfPlayer.hide();
				flvPlayer.show();
				player = flvPlayer;
			}else{
				trace("视频文件不受支持！");
				return;
			}
			
			tree.setSelected(xml);
			player.currentUrl = url;
			if(dlm.isLoaded(url)){
				player.showLoading=false;
				player.load(dlm.getLocalPath(url));
			}else{
				dlm.append(url);
				dlm.load();
				player.stop();
				player.showLoading=true;
				player.setProgress(0, 1);
			}
		}
		
	}
}